package dao.impl;

import cqcet.aibd.soft.ObjectUtil;
import dao.CartDAO;
import model.Trolley;
import model.User;

import java.util.List;

public class CartDAOImpl implements CartDAO {

    @Override
    public int addTrolley(Trolley trolley) {
        String sql = "insert into trolley (id, userId, goodsId,goodsNum,createTime,deleteTime,isDel) values(?,?,?,?,?,?,?)";
        return new ObjectUtil<Trolley>().add(sql,trolley.getId(),trolley.getUserId(),trolley.getGoodsId(),trolley.getGoodsNum(),trolley.getCreateTime(),trolley.getDeleteTime(),trolley.getIsDel());
    }

    @Override
    public void deleteTrolley(String userid, String goodsid) {
        String sql = "delete from trolley where userId=? and goodsId=?";
        new ObjectUtil<User>().delete(sql, userid, goodsid);
    }

    @Override
    public void deleteTrolleyAll(String userID) {
        String sql = "delete from trolley where userId=?";
        new ObjectUtil<User>().delete(sql, userID);
    }

    @Override
    public int updateTrolley(Trolley trolley) {
        String sql = "update trolley set userId=?,goodsId=?,goodsNum=?,createTime=?,deleteTime=?,isDel=? WHERE userId = ? and goodsId=?";
        return new ObjectUtil<Trolley>().update(sql, trolley.getUserId(),trolley.getGoodsId(),trolley.getGoodsNum(),trolley.getCreateTime(),trolley.getDeleteTime(),trolley.getIsDel(),trolley.getUserId(),trolley.getGoodsId());
    }

    @Override
    public List<Trolley> getTrolleyById(String id) {
        String sql = "select * from trolley WHERE isDel='1' and userId=?";
        return new ObjectUtil<Trolley>().getList(sql, Trolley.class, id);
    }

    @Override
    public List<Trolley> getAllTrolley() {
        String sql = "select * from trolley";
        return new ObjectUtil<Trolley>().getList(sql, Trolley.class);
    }
}
